{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 原文地址 [blog.51cto.com](https://blog.51cto.com/zero01/2168999)\n",
    "\n",
    "rancher 是一个开源的企业级全栈化容器部署及管理平台，官方是这么描述的：\n",
    "\n",
    "> Rancher 是业界唯一完全开源的企业级容器管理平台，为企业用户提供在生产环境中落地使用容器所需的一切功能与组件。Rancher2.0 基于 Kubernetes 构建。使用 Rancher，DevOps 团队可以轻松测试、部署和管理应用程序，运维团队可以部署、管理和维护一切 Kubernetes 集群，无论集群运行在何基础设施之上。\n",
    "\n",
    "rancher 官方地址如下：\n",
    "\n",
    "> [https://rancher.com/](https://rancher.com/)\n",
    "\n",
    "中文站点地址如下：\n",
    "\n",
    "> [https://www.cnrancher.com/](https://www.cnrancher.com/)\n",
    "\n",
    "中文文档地址如下：\n",
    "\n",
    "> [https://www.cnrancher.com/docs/rancher/v2.x/cn/overview/quick-start-guide/](https://www.cnrancher.com/docs/rancher/v2.x/cn/overview/quick-start-guide/)\n",
    "\n",
    "本小节我们来学习一下 rancher 的安装，首先我们需要两台虚拟机，一台作为 server，一台作为 agent。两台机器的 ip 分别如下：\n",
    "\n",
    "*   rancher-server : 192.168.190.129\n",
    "*   rancher-server : 192.168.190.130\n",
    "\n",
    "rancher 需要在 Linux 系统上安装，我这里使用的是 CentOS，rancher2.x 要求的 CentOS 版本为 7.5 以上，所以两台机器使用的都是 7.5 版本。rancher 对 docker 版本也有要求，目前只支持 1.12.6、1.13.1 以及 17.03.2 版本。不过我这里冒险用了 18.03.1 版本，发现也是可以用的，当然两台机器的 docker 版本也需要是一样的。如下：\n",
    "\n",
    "```\n",
    "[root@rancher-server ~]\n",
    "CentOS Linux release 7.5.1804 (Core) \n",
    "[root@rancher-server ~]\n",
    "Docker version 18.03.1-ce, build 9ee9f40\n",
    "[root@rancher-server ~]\n",
    "\n",
    "```\n",
    "\n",
    "注：如不熟悉 docker 的安装，可以参考我另一篇文章：[安装 docker 以及通过容器创建镜像](https://blog.51cto.com/zero01/2068220)\n",
    "\n",
    "为了避免端口通信的问题，我们先关闭两台机器的防火墙以及重启一下 docker：\n",
    "\n",
    "```\n",
    "[root@rancher-server ~]\n",
    "[root@rancher-server ~]\n",
    "\n",
    "```\n",
    "\n",
    "如果不关闭防火墙，请参考官方关于端口需求的文档：\n",
    "\n",
    "> [https://www.cnrancher.com/docs/rancher/v2.x/cn/installation/references/](https://www.cnrancher.com/docs/rancher/v2.x/cn/installation/references/)\n",
    "\n",
    "我们首先来安装 rancher server，以下简称 server。在安装 server 之前，我们先配置一下阿里云的 docker 镜像加速地址，否则镜像的下载会很慢：\n",
    "\n",
    "```\n",
    "[root@rancher-server ~]\n",
    "{\n",
    "  \"registry-mirrors\": [\"https://fy707np5.mirror.aliyuncs.com\"]\n",
    "}\n",
    "[root@rancher-server ~]\n",
    "[root@rancher-server ~]\n",
    "\n",
    "```\n",
    "\n",
    "使用官方提供的安装命令进行安装。执行过程如下：\n",
    "\n",
    "```\n",
    "[root@rancher-server ~]\n",
    "Unable to find image 'rancher/rancher:latest' locally\n",
    "latest: Pulling from rancher/rancher\n",
    "124c757242f8: Pull complete \n",
    "2ebc019eb4e2: Pull complete \n",
    "dac0825f7ffb: Pull complete \n",
    "82b0bb65d1bf: Pull complete \n",
    "ef3b655c7f88: Pull complete \n",
    "437f23e29d12: Pull complete \n",
    "52931d58c1ce: Pull complete \n",
    "b930be4ed025: Pull complete \n",
    "4a2d2c2e821e: Pull complete \n",
    "9137650edb29: Pull complete \n",
    "f1660f8f83bf: Pull complete \n",
    "a645405725ff: Pull complete \n",
    "Digest: sha256:6d53d3414abfbae44fe43bad37e9da738f3a02e6c00a0cd0c17f7d9f2aee373a\n",
    "Status: Downloaded newer image for rancher/rancher:latest\n",
    "998306bd217e9790abdeb1fd99f07918e1c85b77687620ef7cb33bd25058b1fb\n",
    "[rancher-server ~]\n",
    "\n",
    "```\n",
    "\n",
    "执行完成后，查看 80、443 端口是否正常监听了：\n",
    "\n",
    "```\n",
    "[root@rancher-server ~]\n",
    "Active Internet connections (only servers)\n",
    "Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    \n",
    "tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      5061/sshd           \n",
    "tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2699/master         \n",
    "tcp6       0      0 :::80                   :::*                    LISTEN      35351/docker-proxy  \n",
    "tcp6       0      0 :::22                   :::*                    LISTEN      5061/sshd           \n",
    "tcp6       0      0 ::1:25                  :::*                    LISTEN      2699/master         \n",
    "tcp6       0      0 :::443                  :::*                    LISTEN      35339/docker-proxy  \n",
    "[root@rancher-server ~]\n",
    "\n",
    "```\n",
    "\n",
    "* * *\n",
    "\n",
    "### 登录 Rancher\n",
    "\n",
    "到此 server 端的安装就算是完成了，接下来我们登录到 server 端的页面上添加集群，使用浏览器访问 rancher server 的主页，第一次访问需要设置管理员密码：  \n",
    "![](https://s4.51cto.com/images/blog/201809/02/b9e26320ef5a003a01e7a5d6e580393c.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)\n",
    "\n",
    "注：因为 Rancher 是自动使用的自签名证书，在第一次登录会提示安全授信问题，信任即可\n",
    "\n",
    "然后设置 rancher server 的 url，我这里就直接使用了虚拟机的内网 ip 了：  \n",
    "![](https://s4.51cto.com/images/blog/201809/02/cbadc80db0d66b0cee0e8a14931651e1.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)\n",
    "\n",
    "完成以上设置后，即可进入到 rancher server 的主页，在这里我们可以设置语言为中文：  \n",
    "![](https://s4.51cto.com/images/blog/201809/02/c4d2baacdc237b84afd6cffcb7d463e5.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)\n",
    "\n",
    "我们首先需要添加一个集群，这里选择 CUSTOM 自定义集群，集群的名称可自定义：  \n",
    "![](https://s4.51cto.com/images/blog/201809/02/c3f43f890722476c3091f10166ffd00b.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)\n",
    "\n",
    "配置主机的及角色地址，这个主机也就是 rancher agent：  \n",
    "![](https://s4.51cto.com/images/blog/201809/02/0f1d27a5e2a9e89b0119d64336366d8a.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)\n",
    "\n",
    "注：如果是多台主机，根据角色的不同，需要复制多次命令\n",
    "\n",
    "复制生成的命令到 rancher agent 机器上执行。执行过程如下：\n",
    "\n",
    "```\n",
    "[root@rancher-agent ~]\n",
    "Unable to find image 'rancher/rancher-agent:v2.0.8' locally\n",
    "v2.0.8: Pulling from rancher/rancher-agent\n",
    "124c757242f8: Pull complete \n",
    "2ebc019eb4e2: Pull complete \n",
    "dac0825f7ffb: Pull complete \n",
    "82b0bb65d1bf: Pull complete \n",
    "ef3b655c7f88: Pull complete \n",
    "9750e7f516aa: Pull complete \n",
    "bbcb46cc1cac: Pull complete \n",
    "f3d67e2639ea: Pull complete \n",
    "4c9aa41b309a: Pull complete \n",
    "64cb19178381: Pull complete \n",
    "Digest: sha256:aa2a164c18ea8b2f6b235186216448a9401ff3e02af064cadea569edc07b45e3\n",
    "Status: Downloaded newer image for rancher/rancher-agent:v2.0.8\n",
    "0356229848f8fd4d832ea5ea9d381a783fc4a67e5c0d1d73882fd1bff353543e\n",
    "[root@rancher-agent ~]\n",
    "\n",
    "```\n",
    "\n",
    "执行成功后，页面下方会显示新主机注册成功：  \n",
    "![](https://s4.51cto.com/images/blog/201809/02/c128859512e028f9ba5165eacdef64dc.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)\n",
    "\n",
    "此时我们的集群处于等待注册的状态，点击主机下的数字可以查看主机信息：  \n",
    "![](https://s4.51cto.com/images/blog/201809/02/89c5b03e1acc415952f86546d2b881b0.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)\n",
    "\n",
    "主机信息如下，这会主机处于注册中的状态：  \n",
    "![](https://s4.51cto.com/images/blog/201809/02/29c4df4aa948f5dd553420576ccf1eff.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)\n",
    "\n",
    "经过一段等待后，主机注册成功，转换为可用状态：  \n",
    "![](https://s4.51cto.com/images/blog/201809/02/97f7138d423c6cde5bdaa0a7d5da86f3.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)\n",
    "\n",
    "点击集群也能够看到集群的仪表盘信息了：  \n",
    "![](https://s4.51cto.com/images/blog/201809/02/a8561853d314cf9c71d52455b5c5bf84.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)\n",
    "\n",
    "集群创建完成后，默认会生成 Default 项目，点击 Default 可以切换到项目视图：  \n",
    "![](https://s4.51cto.com/images/blog/201809/02/0c929e9a85156e891bb0a3a977d58690.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)\n",
    "\n",
    "* * *\n",
    "\n",
    "### 部署工作负载\n",
    "\n",
    "在 rancher 里工作负载是一个对象，包括 pod 以及部署应用程序所需的其他文件和信息。我们这里以 nginx 作为 demo 示例，在 Default 视图下，点击工作负载—部署服务  \n",
    "![](https://s4.51cto.com/images/blog/201809/02/1d096d0efdc845f97d5cc048aec93ff4.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)\n",
    "\n",
    "在部署工作负载页面，设置工作负载名称、副本数量、镜像名称、命名空间、端口映射，其他参数保持默认，不设置端口映射的话，默认是随机映射端口，我这里选择随机，最后点击启动：  \n",
    "![](https://s4.51cto.com/images/blog/201809/02/2465ea3febf19def420175821ad21ad0.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)\n",
    "\n",
    "部署完成：  \n",
    "![](https://s4.51cto.com/images/blog/201809/02/57f2cd0d0d70f928bd05141561116788.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)\n",
    "\n",
    "此时我们可以通过随机映射的 31671 端口去访问 nginx 服务，能访问成功代表我们部署的没有问题：  \n",
    "![](https://s4.51cto.com/images/blog/201809/02/de3ed378cfc0a6f44ad68cac87f484d4.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
